<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Api\Service\Waste\Controllers;


use App\Eloquent\Ygt\Company;
use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\Product;
use App\Eloquent\Ygt\ProductFields;
use App\Eloquent\Ygt\SellerBill;
use App\Eloquent\Ygt\SellerCompany;
use App\Eloquent\Ygt\WasteBill;
use App\Eloquent\Ygt\WasteBuyer;
use App\Engine\Func;
use Framework\BaseClass\Api\Controller;

class CompanyController extends Controller
{
    public $userId        = 261;
    public function index(){}
    public function lists()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        if(!$userInfo)
        {
            xThrow( ERR_PARAMETER );
        }
        $keyword                = request('keyword','');

        $where              = [
            'ygt_waste_buyer.user_id'=>$userId
        ];

        if(!empty($keyword)){
            $where['ygt_waste_buyer.title'] = ['like','%'.$keyword.'%'];
        }

        $limit              = $offset = '';
        $joinType           = 2;
        $collection         = WasteBuyer::getJoinList($where,$limit,$offset,$joinType);
        $lists              = Func::getLetterList($collection);
        return $lists;
    }
    public function detail()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $companyId          = request('id',0);
        if(!$userId)
        {
            xThrow( ERR_PARAMETER );
        }
        $where              = ['user_id'=>$userId,'company_id'=>$companyId];
        $wasteCompany       = WasteBuyer::getInfo($where);
        if(!$wasteCompany)
        {
            xThrow(ERR_UNKNOWN,'你在该厂的信息出错');
        }
        $where              = ['id'=>$companyId];
        $info               = Company::getInfo($where);
        if(!$info)
        {
            xThrow( ERR_PARAMETER,'信息出错' );
        }
        $company            = $info->toArray();
        $result             = [
            'company'=>$company
        ];
        return $company;
    }
    public function billList()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $page               = request('page',1);
        $limit              = request('limit',10);
        $companyId          = request('id',10);
        if(!$userInfo || !$companyId)
        {
            xThrow( ERR_PARAMETER,'登录超时' );
        }
        $where              = ['user_id'=>$userId];
        $wasteBuyer         = WasteBuyer::getInfo($where);
        if(!$wasteBuyer)
        {
            xThrow(ERR_UNKNOWN,'废品单位信息出错');
        }
        $wasteBuyerId       = $wasteBuyer->id;
        $where              = [
            'ygt_waste_bill.company_id'=>$companyId,
            'ygt_waste_bill.waste_buyer_id'=>$wasteBuyerId,
        ];
        $offset             = ($page-1) * $limit;
        $lists              = WasteBill::getJoinList($where,$limit,$offset)->toArray();
        $count              = WasteBill::getCount($where);
        $listsNew           = [];
        foreach($lists as $key=>$val)
        {
            $temp           = [
                'id'=>$val['id'],
                'sn'=>'编号: '.$val['sn'],
                'total_money'=>'¥'.$val['total_money'],
                'now_money'=>'¥'.$val['now_money'],
                'pay_type'=>'付款方式: '.$val['pay_type'],
            ];
            $valImgUrl      = $val['img_url'];
            $imgPath        = '';
            if($valImgUrl)
            {
                $imgPath    = Func::getImgUrlHttp($valImgUrl);
            }
            $temp['img_path']   = $imgPath;

            $valCreatedAt   = $val['created_at'];
            $createdAtYmd   = date('Y-m-d',$valCreatedAt);
            $temp['created_at_ymd'] = '生成日期: '.$createdAtYmd;

            $listsNew[]     = $temp;
        }
        $pagingData         = [
            'page' => $page,
            'pageSize' => $limit,
            'total' => $count,
            'keyword' => ''
        ];
        return $this->pagingData($listsNew, $pagingData);
    }
}